package commands.gameCommand
{
	import Iterator.ArrayIterator;
	import Iterator.IIterator;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.controls.ComboBase;
	
	public class GameboardData extends EventDispatcher
	{
		private var _pieces:Array;
		private var _rows:uint;
		private var _columns:uint;
		private var _newGamePiece:PieceData;
		private static var _iterator:IIterator;
		
		private static var _instance:GameboardData;
		
		public function GameboardData(parameter:SingletoEnforce)
		{
			_rows = 10;
			_columns = 10;
			_newGamePiece = new PieceData();
			_newGamePiece.radius = 40;
			_iterator = GamePlayers.getInstance().iterator();
			update();
		}
		public static function getInstance():GameboardData{
			if(_instance == null){
				_instance = new GameboardData(new SingletoEnforce);
			}
			return _instance;
		}

		public function get row():uint
		{
			return _rows;
		}

		public function set row(value:uint):void
		{
			_rows = value;
			update();
		}

		public function get newGamePiece():PieceData
		{
			return _newGamePiece;
		}

		private function update():void{
			var i:uint;
			var j:uint;
			var piece:PieceData;
			_pieces = new Array();
			
			for(i=0;i<_rows;i++){
				for(j=0;j<_columns;j++){
					piece = new PieceData
					piece.row = i;
					piece.column = j;
					piece.radius = 20;
					addPiece(piece);
				}
			}
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function addPiece(piece:PieceData):void{
			if(_pieces[piece.row] == null){
				_pieces[piece.row] = new Array;
			}
			_pieces[piece.row][piece.column] = piece;
		}
		
		public function iterator():IIterator{
			var pieces:Array = new Array;
			var i:uint;
			var j:uint;
			for(i=0;i<_rows;i++){
				for(j=0;j<_columns;j++){
					pieces.push(_pieces[i][j]);
				}
			}
			return new ArrayIterator(pieces);
		}
		
		public function getProximityPieces(piece:PieceData):IIterator{
			var pieces:Array = new Array;
			var row:uint = piece.row;
			var column:uint = piece.column;
			if(piece.row > 0){
				pieces.push(_pieces[row-1][column]);
				if(piece.row % 2 == 0 && column > 0){
					pieces.push(_pieces[row-1][column-1]);
				}else if(piece.row % 2 != 0 && column < _pieces[row-1].length-1){
					pieces.push(_pieces[row-1][column+1]);
				}
			}
			if(piece.column > 0){
				pieces.push(_pieces[row][column-1]);
			}
			if(piece.column < _pieces[row].length-1){
				pieces.push(_pieces[row][column+1]);
			}
			if(row < _pieces.length-1){
				pieces.push(_pieces[row+1][column]);
				if(row%2 == 0 && column > 0){
					pieces.push(_pieces[row+1][column-1]);
				}else if(row%2 != 0 && column < _pieces[row+1].length-1){
					pieces.push(_pieces[row+1][column+1]);
				}
			}
			
			return new ArrayIterator(pieces);
		}
		public function nextGamePiece():void{
			if(!_iterator.hasNext()){
				_iterator.reset();
			}
			
			_newGamePiece.count = Math.round(Math.random()*18) + 2;
			_newGamePiece.owner = GamePlayer(_iterator.next());
			if(!_iterator.hasNext()){
				_iterator.reset();
			}
		}

		public function get columns():uint
		{
			return _columns;
		}

		public function set columns(value:uint):void
		{
			_columns = value;
		}


	}
}
class SingletoEnforce{}